/*
                       _oo0oo_
                      o8888888o
                      88" . "88
                      (| -_- |)
                      0\  =  /0
                    ___/`---'\___
                  .' \\|     |-- '.
                 / \\|||  :  |||-- \
                / _||||| -:- |||||- \
               |   | \\\  -  --/ |   |
               | \_|  ''\---/''  |_/ |
               \  .-\__  '-'  ___/-. /
             ___'. .'  /--.--\  `. .'___
          ."" '<  `.___\_<|>_/___.' >' "".
         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
         \  \ `_.   \_ __\ /__ _/   .-` /  /
     =====`-.____`.___ \_____/___.-`___.-'=====
                       `=---='


     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

               佛祖保佑         永无BUG

 Made by Alex @7/1/2017 7:52 AM

*/
package main

import (
	"fmt"
	"path"

	"io/ioutil"
	"os"
	"strings"

	"github.com/huandu/xstrings"
	"github.com/kardianos/osext"
	"github.com/koding/multiconfig"
	"github.com/picago/model-generator/core"
	"github.com/picago/model-generator/xerrros"
)

func main() {
	executableForlder, err := osext.ExecutableFolder()
	xerrros.PanicIfError(err)
	fmt.Println(executableForlder)
	_path := path.Join(executableForlder, "k.toml")
	m := multiconfig.NewWithPath(_path)
	config := new(core.Config)
	m.MustLoad(config)
	for _, table := range config.Tables {
		java := core.GenJava(config.Host, table, config.Package)
		dirs := strings.Split(config.Package, ".")
		dir := path.Join(dirs...)
		err := os.MkdirAll(dir, 0744)
		xerrros.PanicIfError(err)
		codeFolder := path.Join(executableForlder, dir)
		err = os.MkdirAll(codeFolder, 0744)
		xerrros.PanicIfError(err)
		filename := path.Join(codeFolder, xstrings.ToCamelCase(table)) + ".java"
		err = ioutil.WriteFile(filename, []byte(java), 0700)
		xerrros.PanicIfError(err)
	}
}
